from utils.tencent.sms import send_sms_single
import random
from django.shortcuts import render, HttpResponse
from django.conf import settings


def send_sms(request):
    """
    发送短信
    ?tpl=login -> 1404299
    ?tpl=register -> 1407218
    """
    tpl = request.GET.get('tpl')
    tpl_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not tpl_id:
        return HttpResponse('模板不存在')
    code = random.randrange(1000, 9999)
    res = send_sms_single('13548726895', tpl_id, [code, ])
    if res['result'] == 0:
        return HttpResponse('发送成功')
    else:
        return HttpResponse(res['errmsg'])
    pass
